package com.lemon.ui.codemaker.window;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import org.apache.commons.lang3.StringUtils;

import com.lemon.components.ant.project.makers.MybatisGenerator;
import com.lemon.components.docs.mysql.JTableDataForMybatisGen;

public class MybatisGenWindow extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTable columnTable;
	private JTable table;
	private JTextField columnsField;

	/**
	 * Create the dialog.
	 */
	public MybatisGenWindow() {
		setType(Type.UTILITY);
		setModal(true);
		setTitle("MybatisGen-选择表");
		setBounds(100, 100, 492, 485);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JScrollPane scrollPane = new JScrollPane();
			contentPanel.add(scrollPane);
			{
				try {
					table = new JTable();
					String[][] tableData = JTableDataForMybatisGen.getData();
					table.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseReleased(MouseEvent e) {
							int[] rownums = table.getSelectedRows();
							StringBuilder fileds = new StringBuilder();
							for(int i=0;i<rownums.length;i++) {
								int rownum = rownums[i];
								if(i>0) {
									fileds.append(",");
								}
								if(rownum >= 0) {
									fileds.append(tableData[rownum][1]);
								}
							}
							columnsField.setText(fileds.toString());
						}
					});
					table.setModel(new DefaultTableModel(
						tableData,
						JTableDataForMybatisGen.getColumns()
					) {
						private static final long serialVersionUID = 9070412818575862413L;

						public boolean isCellEditable(int row, int column) {
							return false;
						}
					});
				} catch (Exception e) {
					JOptionPane.showMessageDialog(contentPanel, e.getMessage(), "数据库出错", JOptionPane.ERROR_MESSAGE);
				}
				table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
				scrollPane.setViewportView(table);
			}
		}
		{
			columnsField = new JTextField();
			columnsField.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					JTextField filterField = (JTextField)e.getSource();
					String[][] filterData;
					if(StringUtils.isNotBlank(filterField.getText())) {
						filterData = JTableDataForMybatisGen.getData(filterField.getText());
					} else {
						filterData = JTableDataForMybatisGen.getData();
					}
					if(filterData.length > 0) {
						table.setModel(new DefaultTableModel(
								filterData,
								JTableDataForMybatisGen.getColumns()
							) {
								private static final long serialVersionUID = 9070412818575862413L;

								public boolean isCellEditable(int row, int column) {
									return false;
								}
							});
					}
					table.addMouseListener(new MouseAdapter() {
						@Override
						public void mouseReleased(MouseEvent e) {
							int[] rownums = table.getSelectedRows();
							StringBuilder fileds = new StringBuilder();
							for(int i=0;i<rownums.length;i++) {
								int rownum = rownums[i];
								if(i>0) {
									fileds.append(",");
								}
								if(rownum >= 0) {
									fileds.append(filterData[rownum][1]);
								}
							}
							columnsField.setText(fileds.toString());
						}
					});
				}
			});
			getContentPane().add(columnsField, BorderLayout.NORTH);
			columnsField.setColumns(10);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("生成Mybatis代码");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						new Thread(new Runnable() {
							
							@Override
							public void run() {
								String columnsString = columnsField.getText();
								String[] columnsName = null;
								if(StringUtils.isNotBlank(columnsString)) {
									columnsName = columnsString.split(",");
								}
								MybatisGenerator.make(columnsName);
							}
						}).start();
					}
				});
				okButton.setActionCommand("gen");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("关闭");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
